//
//  CustomBarController.m
//  Nooryes
//
//  Created by 高博昆 on 15/6/4.
//  Copyright (c) 2015年 Jason. All rights reserved.
//

#import "CustomBarController.h"
#import "CustomeNavigationVC.h"
#import "BaseViewController.h"
#import "NewNewsHomeViewController.h"
#import "MatchHomeViewController.h"


#import "NewNewsHomeViewController.h"

//#import "U9StockHomeViewController.h"
//#import "U9StockAccountViewController.h"
#import "U9ToolsViewController.h"
#import "U9MyViewController.h"

#import "XMGTabBar.h"

#define scheduledTime 30*60

@interface CustomBarController ()<UITabBarControllerDelegate,U9TabBarSelectDelegate>

//@property (nonatomic, strong) UIImageView *myBadgeImage;

@property (nonatomic, strong) MatchHomeViewController *matchHome;

//@property (nonatomic, strong) U9StockHomeViewController *stockVC;
//
//@property (nonatomic, strong) U9StockAccountViewController *stockAccountVC;

@property (nonatomic, strong) U9ToolsViewController *toolsVC;

@property (nonatomic, strong) UIViewController *previousClickedController;

@property (nonatomic, strong) U9MyViewController *myVC;



@end


@implementation CustomBarController

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]  removeObserver:self];
}

+(void)load{
    
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [[UITabBar appearance] setTranslucent:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    
    
    [self addAllTabBarController];
    
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, scheduledTime * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^{
        [self addTimeSelector];
    });
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(modifyGameidAndChangeTabbarButton:) name:@"GameDidChanged" object:nil];
    [self setupTabBar];
}

- (void)addAllTabBarController{
    
    NewNewsHomeViewController *infomationHome = [[NewNewsHomeViewController alloc] init];
    [self addBarItemController:infomationHome title:@"热点" image:@"tabbar_hotPoint" selectedImage:@"tabbar_hotPoint_selected"];
    _infomationHomeViewController = infomationHome;
//    NewNewsHomeViewController *infomationHome = [[NewNewsHomeViewController alloc] init];
//     [self addBarItemController:infomationHome title:@"热点" image:@"tabbar_hotPoint" selectedImage:@"tabbar_hotPoint_selected"];
//     _infomationHomeViewController = infomationHome;
    
    
    MatchHomeViewController *matchHome = [[MatchHomeViewController alloc] init];
    [self addBarItemController:matchHome title:@"赛事" image:@"tabbar_match" selectedImage:@"tabbar_match_selected"];
    _matchHome = matchHome;
    
    U9MyViewController *myVC = [[U9MyViewController alloc] initWithNibName:@"U9MyViewController" bundle:nil];
    [self addBarItemController:myVC title:@"我的" image:@"ico_wode" selectedImage:@"ico_wode_b"];
    _myVC= myVC;
    
    U9ToolsViewController *toolsVC = [[U9ToolsViewController alloc] initWithNibName:@"U9ToolsViewController" bundle:nil];
    [self addBarItemController:toolsVC title:@"工具" image:@"ico_tool" selectedImage:@"ico_tool_b"];
    _toolsVC = toolsVC;
    
    self.previousClickedController = infomationHome;
    
}

- (void)addBarItemController:(UIViewController *)controller title:(NSString *)title
                       image:(NSString *)image selectedImage:(NSString *)selectedImage{
    
    //    controller.tabBarItem.title = title;
    //    controller.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    CustomeNavigationVC *navigationVC = [[CustomeNavigationVC alloc] initWithRootViewController:controller];
    
    [self addChildViewController:navigationVC];
}


- (void)onTabBarClick:(UIButton *)button{
    XMGTabBar *tabBar = (XMGTabBar *)self.tabBar;
    UIButton *preButton = tabBar.previousClickedTabBarButton;
    NSString *string = button.currentTitle;
    
    if ([preButton isEqual:button]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"U9TabbarButtonClickNotification" object:nil];
        //        [(BaseViewController *)self.previousClickedController reloadTableView];
        //        [self.myBadgeImage removeFromSuperview];
        //        self.myBadgeImage = nil;
    }else {
        
        if ([string isEqualToString:@"热点"]) {
            self.selectedIndex = 0;
            self.previousClickedController = self.infomationHomeViewController;
        } else if ([string isEqualToString:@"赛事"]) {
            self.selectedIndex = 1;
            self.previousClickedController = self.matchHome;
        } else if ([string isEqualToString:@"我的"]){
            self.selectedIndex = 2;
            self.previousClickedController = self.myVC;
        } else if ([string isEqualToString:@"工具"]){
            self.selectedIndex = 3;
            self.previousClickedController = self.toolsVC;
        } else { }
        
    }
    tabBar.previousClickedTabBarButton = button;
}

//
//- (void)addBadgeView {
//    if (!self.myBadgeImage) {
//        @try {
//            [[DataHelper sharedDataHelper] getUnReadNumberWithBlock:^(id result) {
//                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
//                if (result && ![result isEqualToString:[userDefault objectForKey:@"readNum"]]) {
//                    self.myBadgeImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"badge"]];
//                    float poisitionX =(self.tabBar.frame.size.width / 8) + 28;
//                    self.myBadgeImage.frame = CGRectMake(poisitionX, 6, 6, 6);
//                    self.myBadgeImage.backgroundColor = [UIColor clearColor];
//                    [self.tabBar addSubview:self.myBadgeImage];
//                    [userDefault setObject:result forKey:@"readNum"];
//                }
//            }];
//        } @catch (NSException *exception) {
//            NSLog(@"%@",exception.reason);
//            NSLog(@"%@",exception.name);
//            NSLog(@"%@",exception.userInfo);
//        } @finally {
//
//        }
//    }
//}

- (void)addTimeSelector{
    //    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:scheduledTime target:self selector:@selector(addBadgeView) userInfo:nil repeats:YES];
    //    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    //    [timer fire];
}


#pragma mark - 自定义tabBar
- (void)setupTabBar {
    NSString *gid = [[NSUserDefaults standardUserDefaults] objectForKey:@"gid"];
    XMGTabBar *tabBar = [XMGTabBar tabBarWithGid:gid];
    tabBar.clickdelegate = self;
    // 替换系统的tabBar KVC:设置readonly属性
    [self setValue:tabBar forKey:@"tabBar"];
    
}


- (void)modifyGameidAndChangeTabbarButton:(NSNotification *)notification{
    XMGTabBar *tabBar = (XMGTabBar *)self.tabBar;
    [tabBar layoutSubviews];
    self.selectedIndex = 0;
    [tabBar selectedFirstButton];
}



//- (void)OrientationDidChange:(NSNotification *)notification{
//    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
//    if (self.myBadgeImage) {
//        if (orientation == 1) {
//            float poisitionX = ceilf(kScreenWidth / 8) + 13;
//            self.myBadgeImage.frame = CGRectMake(poisitionX, 6, 6, 6);
//        }
//    }
//}




#pragma mark - 控制屏幕旋转方法
- (BOOL)shouldAutorotate{
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
@end
